package com.zb.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.common.R;
import com.zb.dto.DishDto;
import com.zb.entity.*;
import com.zb.mapper.CategoryMapper;
import com.zb.mapper.DishFlavorMapper;
import com.zb.mapper.DishMapper;
import com.zb.service.DishFlavorService;
import com.zb.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorService dishFlavorService;

    //添加菜品
    @Override
    @Transactional //涉及到对多张表的数据进行操作,需要加事务，需要事务生效,需要在启动类加上事务注解生效
    public void addDishWithFlavor(DishDto dishDto) {
        // 保存菜品的基本信息到菜品表dish中
        // dishMapper.insert(dishDto);
        this.save(dishDto);
        Long dishId = dishDto.getId();
        // 拿到菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) ->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //这个方法是批量保存
        dishFlavorService.saveBatch(flavors);
    }

    //分页查询菜品
    @Override
    public PageData pageSelect(int page, int pageSize, String name) {
        LambdaQueryWrapper<Dish> dishlambd = new LambdaQueryWrapper<>();
        dishlambd.like(!StringUtils.isEmpty(name), Dish::getName, name);
        Page<Dish> dishPage = new Page<>(page, pageSize);
        IPage<Dish> dishIpage = dishMapper.selectPage(dishPage, dishlambd);

        List<DishDto> dishDtoList = dishIpage.getRecords().stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryMapper.selectById(categoryId);

            if (category != null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }

            return dishDto;
        }).collect(Collectors.toList());
        PageData data = new PageData<DishDto>();
        data.setPage((int) dishIpage.getPages());
        data.setPageSize((int) dishIpage.getSize());
        data.setTotal((int) dishIpage.getTotal());
        data.setList(dishDtoList);
        return data;
    }

    //获取菜品dto详情
    @Override
    public DishDto getDishInfoById(Long id) {

        Dish dish = dishMapper.selectById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> flavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        flavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorMapper.selectList(flavorLambdaQueryWrapper);

        dishDto.setFlavors(flavors);
        return dishDto;
    };


    //更新菜品信息
    @Override
    @Transactional
    public void updateDishInfo(DishDto dishDto) {
        dishMapper.updateById(dishDto);
        LambdaQueryWrapper<DishFlavor> flavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        flavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(flavorLambdaQueryWrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    };

    public List<Dish> getAllDish(Dish dish) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        dishLambdaQueryWrapper.eq(Dish::getStatus, 1); //查询再售状态
        return dishMapper.selectList(dishLambdaQueryWrapper);
    };


}